package com.china08.yxyapi.service.manage.tec.teaching.homework.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.commons.collections.CollectionUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.GetQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.service.manage.tec.teaching.homework.ElasticQuesService;

@Service
public class ElasticQuesServiceImpl implements ElasticQuesService {

	private Logger LOGGER = LoggerFactory.getLogger(ElasticQuesServiceImpl.class);

	@Autowired
	private ElasticsearchTemplate elasticsearchTemplate;

	@Override
	public List<ElasticYxyYxyQuesResource> findByQuesIdIn(List<String> list) {
		List<ElasticYxyYxyQuesResource> elasticQuesList = new ArrayList<ElasticYxyYxyQuesResource>();
		if (CollectionUtils.isNotEmpty(list)) {
			NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
			NativeSearchQuery query = builder.withIds(list).build();
			elasticQuesList = elasticsearchTemplate.multiGet(query, ElasticYxyYxyQuesResource.class);
		}
		return elasticQuesList;
	}

	@Override
	public ElasticYxyYxyQuesResource findByEsQuesId(String quesId) {
		GetQuery getQuery = new GetQuery();
		getQuery.setId(quesId);
		ElasticYxyYxyQuesResource elasticQues = elasticsearchTemplate.queryForObject(getQuery,
				ElasticYxyYxyQuesResource.class);
		return elasticQues;
	}

	@Override
	public ElasticYxyYxyQuesResource findByQuestionTypeAndQuesIdIn(String questionType, List<Integer> knowIdList,
			String difficultLevel, List<String> quesIdList) {

		BoolQueryBuilder boolBuilder = new BoolQueryBuilder();
		TermsQueryBuilder builder = new TermsQueryBuilder("knowledgeIds", knowIdList);
		boolBuilder.must(builder);
		TermQueryBuilder builderLevel = new TermQueryBuilder("difficultyLevel", difficultLevel);
		TermQueryBuilder builderType = new TermQueryBuilder("questionType", questionType);
		boolBuilder.must(builderLevel);
		boolBuilder.must(builderType);
		TermsQueryBuilder mustEsQuesId = new TermsQueryBuilder("id", quesIdList);
		boolBuilder.mustNot(mustEsQuesId);
		NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder();
		NativeSearchQuery nativeSearchQuery = searchQueryBuilder.withQuery(boolBuilder)
				.withPageable(new PageRequest(0, 100)).build();
		List<ElasticYxyYxyQuesResource> respElasticQues = elasticsearchTemplate.queryForList(nativeSearchQuery,
				ElasticYxyYxyQuesResource.class);
		//
		LOGGER.info(boolBuilder.toString() + "================");
		// 随机拿一道题
		ElasticYxyYxyQuesResource basicQues = null;
		if (CollectionUtils.isNotEmpty(respElasticQues)) {
			int max = respElasticQues.size();
			Random random = new Random();
			int r = random.nextInt(max);
			basicQues = respElasticQues.get(r);
		}
		return basicQues;
	}

	@Override
	public List<ElasticYxyYxyQuesResource> findQuesListByChapter(String[] questionType, List<Integer> knowIdList,
			String difficultLevel, Integer count) {
		long a = System.currentTimeMillis();
		BoolQueryBuilder boolBuilder = new BoolQueryBuilder();
		TermsQueryBuilder builder = new TermsQueryBuilder("knowledgeIds", knowIdList);
		boolBuilder.must(builder);
		TermQueryBuilder builderLevel = new TermQueryBuilder("difficultyLevel", difficultLevel);
		boolBuilder.must(builderLevel);
		TermsQueryBuilder builderType = null;
		if (questionType.length > 1) {
			builderType = new TermsQueryBuilder("questionType", questionType);
			boolBuilder.mustNot(builderType);
		} else {
			builderType = new TermsQueryBuilder("questionType", questionType);
			boolBuilder.must(builderType);
		}
		TermQueryBuilder viewScopeBuilder = new TermQueryBuilder("viewScope", 0);
		boolBuilder.mustNot(viewScopeBuilder);
		NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder();
		List<ElasticYxyYxyQuesResource> respElasticQues = new ArrayList<ElasticYxyYxyQuesResource>();
		if (count>0) {
			NativeSearchQuery nativeSearchQuery = searchQueryBuilder.withQuery(boolBuilder)
					.withPageable(new PageRequest(0, count)).build();
			respElasticQues = elasticsearchTemplate.queryForList(nativeSearchQuery,
					ElasticYxyYxyQuesResource.class);
		}
		LOGGER.info(boolBuilder.toString() + "================");
		// 生成不重复的n个随机数
		/*List<Integer> radomNum = new ArrayList<Integer>();
		if (count < respElasticQues.size()) {
			for (int m = 0; m < count; m++) {
				int max = respElasticQues.size();
				Random random = new Random();
				int r = random.nextInt(max);
				boolean flag = false;
				if (radomNum.contains(r)) {
					flag = true;
				}
				while (flag) {
					max = max - 1;
					r = random.nextInt(max);
					if (radomNum.contains(r)) {
						flag = true;
					} else {
						flag = false;
					}
				}
				radomNum.add(r);
			}
		} else {
			for (int n = 0; n < respElasticQues.size(); n++) {
				radomNum.add(n);
			}
		}*/
		// 返回响应
		/*List<ElasticYxyYxyQuesResource> returnQuesList = new ArrayList<ElasticYxyYxyQuesResource>();
		ElasticYxyYxyQuesResource basicQues = null;
		for (Integer integer : radomNum) {
			basicQues = respElasticQues.get(integer);
			returnQuesList.add(basicQues);
		}*/
		System.out.println("A" + ((System.currentTimeMillis() - a) / 1000));
		return respElasticQues;
	}

}
